package org.example.service.spellcheck;

import org.example.service.spellcheck.SpellChecker;
import org.languagetool.JLanguageTool;
import org.languagetool.language.AmericanEnglish;
import org.languagetool.rules.RuleMatch;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class LanguageToolSpellChecker implements SpellChecker {
    private final JLanguageTool langTool;

    public LanguageToolSpellChecker() {
        this.langTool = new JLanguageTool(new AmericanEnglish());
    }

    @Override
    public List<String> checkSpelling(String text) throws IOException {
        List<String> corrections = new ArrayList<>();
        List<RuleMatch> matches = langTool.check(text);

        for (RuleMatch match : matches) {
            String error = "Potential error at characters " +
                    match.getFromPos() + "-" + match.getToPos() + ": " +
                    match.getMessage();
            String suggestions = "Suggested correction(s): " + match.getSuggestedReplacements();
            corrections.add(error + "\n" + suggestions);
        }
        return corrections;
    }
}
